package com.shujia.mr.kqzl;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class KQZLReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        long count = 0;
        long sum = 0;

        for (LongWritable value : values) {
            long pm25 = value.get();
            if (pm25 != 0) {
                count++;
                sum += pm25;
            }
        }

        if(count==0){
            context.write(key,new LongWritable(0));
        }else {
            context.write(key,new LongWritable(sum/count));
        }


    }
}
